const express = require('express');
const pool = require('../../pool.js');
const r = express.Router();
r.get('/show', (req, res, next) => {
    pool.query('select * from goods where id=83', (err, data) => {
        if (err) { next(err); return; }
        res.send({
            "code": 1,
            "msg": "已查到所有数据",
            "data": data
        })
    })
})
// 立即购买后插入数据到订单
r.post('/orderinsert', (req, res, next) => {
    let { count, pic, title, size, price_total, type} = req.body
    let order_status = "待发货";
    let oid = Math.floor(Math.random() * 1000000000000000)
    let shopId ='12345'
    let user='123123'
    pool.query('insert orders set order_status=?,count=?,pic=?,title=?,size=?,price_total=?,oid=?,type=?,shopId=?,user=?', [order_status, count, pic, title, size, price_total, oid, type, shopId,user], (err, data) => {
        if (err) { throw err; }
        res.send(data)
        // console.log(data);
    })
})
// 加入购物车后插入数据到购物车
r.post('/cartinsert', (req, res, next) => {
    let { shop_id, number, total, price, pic, size, title, clostyle } = req.body
    pool.query('insert cart set shop_id=?,number=?,total=?,price=?,pic=?,size=?,title=?,clostyle=?', [shop_id, number, total, price, pic, size, title, clostyle], (err, data) => {
        if (err) { throw err; }
        res.send(data)
        // console.log(data);
    })
})
// 查询订单数据
r.get('/myorders', (req, res) => {
    let user = req.query.user;
    let a = '运输中';
    let c = '已送达';
    if (req.query.select == '0') {
        // 显示除了运输中和已送达的所有订单
        pool.query('select * from orders where user = ? and order_status != ? and order_status != ? ', [user, a, c], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    } else if (req.query.select == '1') {
        // 待收货    '运输中'在待收货中显示，点击确认收货之后显示 '已送达'
        pool.query('select * from orders where user = ? and order_status = ?  ', [user, a], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    } else if (req.query.select == '2') {
        // 待评价  '已送达'在待评价页面中显示    评论完成之后  显示'已完成'，在全部订单中查看
        pool.query('select * from orders where user = ? and order_status = ?  ', [user, c], (err, r) => {
            if (err) {
                throw err;
            }
            res.send(r);
        })
    }
})
r.get('/shopuser', (req, res) => {
    pool.query('select * from user where shopId = ?', [req.query.shopId], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 收货
r.get('/acceptorder', (req, res) => {
    let order_status = '已送达'
    pool.query('update orders set order_status=? where id =?', [order_status, req.query.id], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 退货
r.put('/exitorder', (req, res) => {
    let aftersales = req.body.aftersales;
    let aftercause = req.body.aftercause;
    let id = req.body.id;
    let order_status = '待处理';
    pool.query('update orders set order_status=?,aftersales=?,aftercause=? where id=?', [order_status, aftersales, aftercause, id], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
//删除已完成订单
r.delete('/delorder', (req, res) => {
    let id = req.query.id;
    pool.query('delete from orders where id = ?', [id], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 评价订单
r.post('/comment', (req, res) => {
    let userComment = req.body.userComment;
    let oid = req.body.oid;
    let user = req.body.user;
    let grade = req.body.grade;
    pool.query('insert comments set userComment=?,user=?,oid=?', [userComment, user, oid], (err, r) => {
        if (err) {
            throw err;
        }
        if (r) {
            let order_status = '已完成';
            pool.query('update orders set order_status=?,grade=? where oid =?', [order_status, grade, oid], (err, data) => {
                if (err) {
                    throw err;
                }
                res.send(data);
            })
        }
    })
});
// 城市定位
r.get('/city', (req, res) => {
    let city = req.query.city;
    let name = req.query.Name;
    pool.query('select lng,Lat  from areas where CityShortName = ? and Name=?', [city, name], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
// 用户信息
r.get('/customer', (req, res) => {
    let uname = req.query.uname;
    pool.query('select uaddr from customer where uname =?', [uname], (err, r) => {
        if (err) {
            throw err;
        }
        res.send(r);
    })
})
module.exports = r;